<!-- components/ProductDetail.vue -->
<template>
    <div class="product-detail">
        <h1>商品详情</h1>
        <div v-if="product">
            <div class="product-image">
                <img :src="product.image" alt="商品图片" />
            </div>
            <div class="product-info">
                <h2>{{ product.name }}</h2>
                <p>价格：{{ product.price }}</p>
                <p>销量：{{ product.sales }}</p>
                <p>{{ product.content }}</p>
                <p>{{ product.features }}</p>
            </div>
        </div>
    </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { useRoute } from 'vue-router';
import axios from 'axios';

const route = useRoute();
const product = ref(null);

onMounted(() => {
    const productId = route.params.id;
    axios.get(`http://127.0.0.1:8000/app/goods/${productId}/`).then(res => {
        product.value = res.data;
        recordBrowse(productId);
    })
});
const recordBrowse = async (product_id) => {
    try {
        await axios.post(`http://127.0.0.1:8000/app/browse/${product_id}/`);
    } catch (error) {
        console.error('Error recording browse:', error);
    }
};

</script>

<style>
.product-detail {
    max-width: 1200px;
    margin: 0 auto;
    padding: 20px;
    font-family: Arial, sans-serif;
    background-color: #f4f4f4;
    border-radius: 10px;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

h1 {
    text-align: center;
    color: #333;
    margin-bottom: 20px;
    font-size: 2em;
}

.product-image {
    width: 100%;
    height: 400px;
    overflow: hidden;
    border-radius: 8px;
    margin-bottom: 20px;
}

.product-image img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.product-info {
    text-align: left;
}

.product-info h2 {
    font-size: 1.8em;
    margin: 0;
    color: #333;
}

.product-info p {
    margin: 10px 0;
    color: #555;
}

.product-info .price {
    font-weight: bold;
    color: #ff5722;
}

.product-info .sales {
    font-style: italic;
}
</style>